<?php
/**
 * 前台专用控制器
、 * 设置默认的视图目录,设置默认视图扩展名为php
 * @package front
 */
class UserAction extends Request
{
    
	/**
	 * 系统配置信息
	 * 
	 * @var Array
	 */
    public $cfg;
    
    /**
     * 数据库对象
     *
     * @var Mysql
     */
    public $db;
    
    /**
     * 用户编号
     *
     * @var Integer
     */
    protected $uid;
    
    /**
     * 用户信息
     * @var Array
     */
    protected $uinfo;
    
    protected $baseUrl;
    
    /**
     * 重载控制器
     * 定义默认的视图路径，文件编码，视图扩展名
     * 并调用权限认证
     */
    public function __construct (Route $route) {
        parent::__construct($route);
        $this->setViewPath(dirname ($route->getControllerPath ()) . '/views')//设置视图路径
             ->setAutoRender (false)//设置是否自动渲染页面
             ->setSuffix ('.php');//设置视图后缀名
        
        $controllerName         = $this->getParam('controller');
        $actionName             = $this->getParam('action');
        
        $this->view->baseUrl    = $this->getHost() . $this->getBaseUrl ();
        $this->view->fullUrl    = $this->getHost() . $this->getBaseUrl ();
        $this->view->controllerName = $controllerName;
        $this->baseUrl = $this->view->baseUrl;
        
        global $cfg;
        $this->cfg = $cfg;
        $this->view->cfg = $cfg;
        $this->db = Mysql::getInstance();
        
        $this->auth($controllerName, $actionName);
        $this->initUinfo();
    }
    
    // 登录判断
    private function auth($controllerName, $actionName)
    {
    	$auth = true;

    	// 配置忽略权限检测
    	$arr_ignore = array();
    	$arr_ignore['meet']['index'] = true;
    	
    	if (isset($arr_ignore[$controllerName]))
    		$auth = false;
    	
    	if ($auth && empty($_SESSION['uid'])){
    		$this->redirectAction("index", "index");
    	}
    	
    	$this->uid = (int)$_SESSION['uid'];
    }
    
    /**
     * 获取用户信息
     */
    private function initUinfo()
    {
    	$uid = (int)$_SESSION['uid'];
    	 
    	$this->uinfo = $this->db->fetchRow("select * from user where uid=?", array($uid));
    	 
    	$this->view->uinfo = $this->uinfo;
    }
    
    /**
     * 输出错误信息(json)
     */
    public function showmessage ($message='', $type='') {
        $message = array("status" => 0, "message" => $message);
        if($type != "")
        	$message['type'] = $type;
    	
    	echo json_encode($message);
    	exit;
    }
    
    /**
     * 内部跳转
     *
     * @param unknown_type $controller
     * @param unknown_type $action
     * @param unknown_type $param
     */
    protected function redirectAction($controller, $action, $param = '') {
    	$url = $this->url($this->view->baseUrl, $controller, $action, $param);
    	header("location:" . $url);
    	exit;
    }
    
    /**
     * 显示信息，并跳转(html)
     */
    public function showtipmessage ($url = "", $msg = "") {
    	if (!empty($url))
    		$this->view->url = $url;
    	if (!empty($msg))
    		$this->view->msg = $msg;
    
    	$this->renderScript (APP . '/application/views/tipmessage.php');
    }
}